package com.boarsoft.boar.batch.demo.biz;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.batch.demo.entity.Coredpfm01;
import com.boarsoft.dao.sharding.bean.DaoSharding;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component
public class Coredpfm01BizImpl extends SimpleBizImpl implements Coredpfm01Biz {
	public static final String INSERT_SQL = "insert into coredpfm01 (id, DM01ACCT, DM01CLSFLG, DM01DOCLST, DM01PSWLST, DM01ACTFLG, DM01SECFLG, DM01DOCFLG, DM01VOUCLS, DM01VOUNUM, DM01WTTYPE, DM01WTPRF1, DM01INQPAS, DM01WTPRF3, DM01PRTNUM, DM01UPRTNU, DM01ACCNUM, DM01ACCSEQ, DM01ORGNO, DM01CLIPRO, DM01PBNUM, DM01LICNO, DM01AUTNO, DM01AVLDAT, DM01FLAG, DM01NODE, LAST_ETL_ACG_DT, DEL_F) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

	@Autowired
	@Qualifier("dataSource")
	@Lazy
	private DataSource datasource;

	@Override
	@DaoSharding(policy = "user", index = 0)
	@Transactional
	public void save(int tenantId, Coredpfm01 dpfm) {
		dao.save(dpfm);
	}

	@Override
	@DaoSharding(policy = "user", index = 0)
	// @Transactional
	public void batchSave(int tenantId, List<Coredpfm01> lt) throws SQLException {
		// dao.batchSave(lt, 50);
		Connection conn = null;
		try {
			conn = datasource.getConnection();
			PreparedStatement ps = conn.prepareStatement(INSERT_SQL);
			conn.setAutoCommit(false);
			for (int i = 0; i < lt.size(); i++) {
				Coredpfm01 o = lt.get(i);
				ps.setString(1, o.getId());
				ps.setString(2, o.getDM01ACCT());
				ps.setString(3, o.getDM01CLSFLG());
				ps.setString(4, o.getDM01DOCLST());
				ps.setString(5, o.getDM01PSWLST());
				ps.setString(6, o.getDM01ACTFLG());
				ps.setString(7, o.getDM01SECFLG());
				ps.setString(8, o.getDM01DOCFLG());
				ps.setString(9, o.getDM01VOUCLS());
				ps.setString(10, o.getDM01VOUNUM());
				ps.setString(11, o.getDM01WTTYPE());
				ps.setString(12, o.getDM01WTPRF1());
				ps.setString(13, o.getDM01INQPAS());
				ps.setString(14, o.getDM01WTPRF3());
				ps.setBigDecimal(15, o.getDM01PRTNUM());
				ps.setBigDecimal(16, o.getDM01UPRTNU());
				ps.setBigDecimal(17, o.getDM01ACCNUM());
				ps.setBigDecimal(18, o.getDM01ACCSEQ());
				ps.setString(19, o.getDM01ORGNO());
				ps.setString(20, o.getDM01CLIPRO());
				ps.setBigDecimal(21, o.getDM01PBNUM());
				ps.setString(22, o.getDM01LICNO());
				ps.setString(23, o.getDM01AUTNO());
				ps.setString(24, o.getDM01AVLDAT());
				ps.setString(25, o.getDM01FLAG());
				ps.setString(26, o.getDM01NODE());
				ps.setString(27, o.getLAST_ETL_ACG_DT());
				ps.setLong(28, o.getDEL_F());
				ps.addBatch();
				if (i % 100 == 0) {
					ps.executeBatch();
					conn.commit();
				}
			}
			ps.executeBatch();
			conn.commit();
		} finally {
			if (conn != null) {
				conn.close();
			}
		}
	}

	@Override
	@DaoSharding(policy = "user", index = 0)
	@Transactional(readOnly = true)
	public List<Coredpfm01> list(int tenantId, String hql, int pageNo, int pageSize) {
		return dao.list(hql, pageNo, pageSize);
	}
}
